package com.example.bangbangxia.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.bangbangxia.domain.Task;
import com.example.bangbangxia.service.TaskService;
import com.example.bangbangxia.utils.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 任务的控制类
 */
@Api(tags = "任务相关接口")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService taskService;


    /**
     * 添加任务
     * @param task
     * @return
     */
    @PostMapping(value = "/insertTask")
    public RespBean insertTask(@RequestBody Task task){
        return taskService.insertTask(task);
    }

    /**
     * 删除任务
     * @return
     */
    @DeleteMapping(value = "/deleteTask")
    public RespBean deleteTask(Integer taskId){
        return taskService.deleteTask(taskId);
    }

    /**
     * 更新任务
     * @param task
     * @return
     */
    @PutMapping(value = "updateTask")
    public RespBean updateTask(@RequestBody Task task){
        return taskService.updateTask(task);
    }
    /**
     * 查询所有任务
     * @return
     */
    @ApiOperation("查询任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "0",paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "5",paramType = "Integer"),
            @ApiImplicitParam(name = "userId",paramType = "Integer",required = true)
    })
    @GetMapping(value = "/queryTaskList/{currentPage}/{pageSize}/{userId}")
    public RespBean queryTaskList(@PathVariable Integer currentPage,
                                  @PathVariable Integer pageSize,
                                  @PathVariable Integer userId){
        if (currentPage==null){
            currentPage=1;
        }
        if (pageSize==null){
            pageSize=5;
        }
        return taskService.queryTaskList(currentPage,pageSize,userId);
    }


    /**
     * 查看自己发布的任务（1.未被接/2.正在进行中/3.已完成）/ 查看自己接受的任务（2.正在进行中/3.已完成）
     * @return
     */
    @PostMapping(value = "/queryMyTask")
    public RespBean queryMyTask(@RequestParam(defaultValue = "1")Integer currentPage,
                                @RequestParam(defaultValue = "5")Integer pageSize,
                                Integer userId,Integer taskState,Integer acceptUserId){
        return taskService.queryMyTask(currentPage,pageSize,userId,taskState,acceptUserId);
    }

    @ApiOperation("接收任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "task",required = true),
            @ApiImplicitParam(name="myId",required = true)
    })
    @PutMapping(value = "/acceptTask")
    public RespBean acceptTask(@RequestBody JSONObject requestJSON){
        Task task = requestJSON.getObject("task", Task.class);
        Integer myId= (Integer) requestJSON.get("myId");
        //判空
        if (task==null || myId==null){
            return RespBean.error("参数不完整");
        }
        return taskService.acceptTask(task,myId);
    }
}
